package cn.whpu.omnitrixerp.util;


import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Date;

/**
 * @author Gim Gong
 * @date 2025/6/29
 */
public class JwtUtil {

    /**
     * 过期时间 1h
     */
    private static final long EXPIRATION_TIME = 1000 * 60 * 60;

    /**
     * 密钥
     */
    private static final String SECRET_KEY = "ZCEQIUBFKSJBFJH2020BQWE";


    /**
     * 生成token
     *
     * @param userDetails 登录请求对象
     * @return token字符串
     */
    public static String generateToken(UserDetails userDetails) {
        Date now = new Date();
        Date expiration = new Date(now.getTime() + EXPIRATION_TIME);
        return JWT.create()
                .withIssuedAt(now)
                .withSubject(userDetails.getUsername())
                .withExpiresAt(expiration)
                .sign(Algorithm.HMAC256(SECRET_KEY));
    }

    /**
     * 验证Token
     *
     * @param token token字符串
     * @param userDetails 用户信息
     * @return 验证结果
     */
    public static boolean validateToken(String token, UserDetails userDetails) {
        try {
            Algorithm algorithm = Algorithm.HMAC256(SECRET_KEY);
            JWTVerifier verifier = JWT.require(algorithm)
                    .withSubject(userDetails.getUsername())
                    .build();
            verifier.verify(token);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 获取用户名
     *
     * @param token token字符串
     * @return 用户名
     */
    public static String extractUsername(String token) {
        try {
            DecodedJWT jwt = JWT.decode(token);
            return jwt.getSubject();
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 判断Token是否过期
     *
     * @param token token字符串
     * @return 是否过期
     */
    public static boolean isTokenExpired(String token) {
        try {
            DecodedJWT jwt = JWT.decode(token);
            return jwt.getExpiresAt().before(new Date());
        } catch (Exception e) {
            return true;
        }
    }


    public static void main(String[] args) {
        // // 创建要存储的声明信息
        // UserPO userPO = new UserPO();
        // userPO.setUsername("admin");
        // userPO.setPassword("123456");
        //
        // // 生成Token
        // String token = generateToken(userPO);
        // System.out.println("生成的Token: " + token);
        //
        // try {
        //     // 验证Token
        //     Map<String, Object> claimsFromToken = validateToken(token);
        //     System.out.println("Token验证通过");
        //     System.out.println("用户名: " + claimsFromToken.get("username"));
        //     System.out.println("密码: " + claimsFromToken.get("password"));
        // } catch (Exception e) {
        //     System.out.println("Token验证失败: " + e.getMessage());
        // }
    }
}
